package org.jeecg.modules.demo.checkemployee.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.base.entity.ZnzfScoreStandardItem;
import org.jeecg.modules.demo.checkemployee.entity.ZnzfCheckEmployee;
import org.jeecg.modules.demo.checkemployee.service.IZnzfCheckEmployeeService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.demo.definedtool.Paging;
import org.jeecg.modules.demo.roomapartdistribute.entity.ZnzfCheckApart;
import org.jeecg.modules.demo.roomapartdistribute.entity.ZnzfRoomApartDistribute;
import org.jeecg.modules.demo.roomapartdistribute.service.IZnzfCheckApartService;
import org.jeecg.modules.demo.roomapartdistribute.service.IZnzfRoomApartDistributeService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 核定单位报名人员
 * @Author: jeecg-boot
 * @Date:   2022-03-21
 * @Version: V1.0
 */
@Api(tags="核定单位报名人员")
@RestController
@RequestMapping("/checkemployee/znzfCheckEmployee")
@Slf4j
public class ZnzfCheckEmployeeController extends JeecgController<ZnzfCheckEmployee, IZnzfCheckEmployeeService> {
	@Autowired
	private IZnzfCheckEmployeeService znzfCheckEmployeeService;

	 @Autowired
	 private ISysUserService sysUserService;

	 @Autowired
	 private IZnzfCheckApartService znzfCheckApartService;

	 @Autowired
	 private IZnzfRoomApartDistributeService znzfRoomApartDistributeService;

	 @Autowired
	 private ISysDepartService sysDepartService;
	
	/**
	 * 分页列表查询
	 *
	 * @param znzfCheckEmployee
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "核定单位报名人员-分页列表查询")
	@ApiOperation(value="核定单位报名人员-分页列表查询", notes="核定单位报名人员-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<ZnzfCheckEmployee>> queryPageList(ZnzfCheckEmployee znzfCheckEmployee,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ZnzfCheckEmployee> queryWrapper = QueryGenerator.initQueryWrapper(znzfCheckEmployee, req.getParameterMap());
		Page<ZnzfCheckEmployee> page = new Page<ZnzfCheckEmployee>(pageNo, pageSize);
		IPage<ZnzfCheckEmployee> pageList = znzfCheckEmployeeService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param znzfCheckEmployee
	 * @return
	 */
	@AutoLog(value = "核定单位报名人员-添加")
	@ApiOperation(value="核定单位报名人员-添加", notes="核定单位报名人员-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ZnzfCheckEmployee znzfCheckEmployee) {
		znzfCheckEmployeeService.save(znzfCheckEmployee);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param znzfCheckEmployee
	 * @return
	 */
	@AutoLog(value = "核定单位报名人员-编辑")
	@ApiOperation(value="核定单位报名人员-编辑", notes="核定单位报名人员-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody ZnzfCheckEmployee znzfCheckEmployee) {
		znzfCheckEmployeeService.updateById(znzfCheckEmployee);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "核定单位报名人员-通过id删除")
	@ApiOperation(value="核定单位报名人员-通过id删除", notes="核定单位报名人员-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		znzfCheckEmployeeService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "核定单位报名人员-批量删除")
	@ApiOperation(value="核定单位报名人员-批量删除", notes="核定单位报名人员-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.znzfCheckEmployeeService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "核定单位报名人员-通过id查询")
	@ApiOperation(value="核定单位报名人员-通过id查询", notes="核定单位报名人员-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ZnzfCheckEmployee> queryById(@RequestParam(name="id",required=true) String id) {
		ZnzfCheckEmployee znzfCheckEmployee = znzfCheckEmployeeService.getById(id);
		if(znzfCheckEmployee == null) {
			return Result.error("未找到对应数据",null);
		}
		return Result.OK(znzfCheckEmployee);
	}

	/**
	 * 通过id查询用户的得分详情
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="核定单位报名人员-通过id查询用户的得分详情", notes="核定单位报名人员-通过id查询用户的得分详情")
	@GetMapping(value = "/queryScoreDetailsById")
	public Result<List<ZnzfScoreStandardItem>> queryScoreDetailsById(@RequestParam(name="id",required=true) String id) {
		List<ZnzfScoreStandardItem> znzfScoreStandardItemList = znzfCheckEmployeeService.queryScoreDetailsById(id);
		if(znzfScoreStandardItemList == null) {
			return Result.error("未找到对应数据",null);
		}
		return Result.OK(znzfScoreStandardItemList);
	}

	/**
	 * @Description: 根据房源单位分配表的id查询
	 *
	 * @date: 2022/3/23 15:23
	 * @param radId: 房源单位分配表的id
	 * @Return: com.alibaba.fastjson.JSONArray
	 */
	@ApiOperation(value="核定单位报名人员-通过radId查询", notes="核定单位报名人员-通过radId查询")
	@GetMapping(value = "/queryByRadId")
	public Result<JSONArray> queryByRadId(@RequestParam(name="radId",required=true) String radId) {
		JSONArray jsonArray = znzfCheckEmployeeService.getByRadId(radId);
		if (jsonArray == null) {
			return Result.error("未找到对应数据", null);
		}
		return Result.OK(jsonArray);
	}

	 /**
	  * 通过radId和查询人的部门查询核定单位报名人员
	  * @param radId
	  * @return
	  */
	 @ApiOperation(value="核定单位报名人员-通过radId和查询人的部门查询", notes="核定单位报名人员-通过radId和查询人的部门查询")
	 @GetMapping(value = "/queryByRadIdAndApartId")
	 public Result<Page<ZnzfCheckEmployee>> queryByRadIdAndApartId(HttpServletRequest request, @RequestParam(name="radId",required=true) String radId,
																   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
																   @RequestParam(name="unit", required = false) String name) {
		 String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
		 String name1 = JwtUtil.getUsername(token);
		 SysUser sysUser = sysUserService.getUserByName(name1);
		 if(name1 == null || sysUser == null)
			 return Result.error("账号异常");
		 ZnzfRoomApartDistribute znzfRoomApartDistribute = znzfRoomApartDistributeService.getById(radId);
		 if(znzfRoomApartDistribute == null)
			 return Result.error("房源单位分配异常");
		 SysDepart sysDepart = sysDepartService.searchByOrgCode(sysUser.getOrgCode());
		 ZnzfCheckApart znzfCheckApart = znzfCheckApartService.searchByZnzfRoomApartDistributeIdAndApartId(radId, sysDepart.getId());
		 if(znzfCheckApart == null)
			 return Result.error("核定部门异常");
		 List<ZnzfCheckEmployee> znzfCheckEmployeeList = znzfCheckEmployeeService.getByCheckApartId(znzfCheckApart.getId());
		 if (znzfCheckEmployeeList == null) {
			 return Result.error("未找到对应数据", null);
		 }
		 List<ZnzfCheckEmployee> result = new ArrayList<>();
		 if(!znzfCheckEmployeeList.isEmpty()){
		 	for(ZnzfCheckEmployee znzfCheckEmployee : znzfCheckEmployeeList){
		 		String realName = znzfCheckEmployee.getRealName();
		 		if(name == null || realName.contains(name))
		 			result.add(znzfCheckEmployee);
			}
		 }
		 Paging<ZnzfCheckEmployee> paging = new Paging<>();
		 return paging.getPaging(result, pageNo, pageSize);
	 }

    /**
    * 导出excel
    *
    * @param request
    * @param znzfCheckEmployee
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ZnzfCheckEmployee znzfCheckEmployee) {
        return super.exportXls(request, znzfCheckEmployee, ZnzfCheckEmployee.class, "核定单位报名人员");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ZnzfCheckEmployee.class);
    }

}
